package com.ruitu.health_2.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.health_2.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

// 聊天页面
public class ChatActivity extends BaseActivity {
    private RecyclerView rcv_list;// 消息列表
    private EditText et_content;// 输入的内容
    private TextView tv_send;// 发送

    private List<String> chatMsgList = new ArrayList<>();// 聊天消息列表
    private ChatMsgListAdapter adapter;// 聊天消息adapter

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_chat;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rcv_list = findViewById(R.id.rcv_list);
        et_content = findViewById(R.id.et_content);
        tv_send = findViewById(R.id.tv_send);

        setOnClickListener(tv_send);

        adapter = new ChatMsgListAdapter(this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setStackFromEnd(true);
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);

        setHeadTitle("用户名");
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_send) {// 发送
            String content = et_content.getText().toString().trim();
            if (TextUtils.isEmpty(content)) {
                return;
            }
            ToastUtils.showShortSafe("发送消息:" + content);
            chatMsgList.add(content);
//            Collections.reverse(chatRecoList);
            adapter.setDataList(chatMsgList);
            rcv_list.postDelayed(new Runnable() {
                @Override
                public void run() {
                    rcv_list.smoothScrollToPosition(chatMsgList.size());
                }
            }, 100);
            et_content.setText("");// 清空之前的内容
        }
    }

    private static class ChatMsgListAdapter extends ListBaseAdapter<String> {

        public ChatMsgListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getItemViewType(int position) {
            return super.getItemViewType(position);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_chat_msg_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_time = holder.getView(R.id.tv_time);
            TextView tv_content = holder.getView(R.id.tv_content);

            tv_time.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            tv_content.setText(mDataList.get(position));
        }
    }
}
